package com.acmedcare.framework.exchange.center.endpoint.service;

import com.acmedcare.framework.boot.snowflake.Snowflake;
import com.acmedcare.framework.exchange.center.common.ConvertUtils;
import com.acmedcare.framework.exchange.center.common.Page;
import com.acmedcare.framework.exchange.center.endpoint.exception.InvalidRequestParameterException;
import com.acmedcare.framework.exchange.center.entity.bean.Stage;
import com.acmedcare.framework.exchange.center.entity.bean.StageConfig;
import com.acmedcare.framework.exchange.center.entity.dto.StageDetailDto;
import com.acmedcare.framework.exchange.center.entity.dto.StageQueryDto;
import com.acmedcare.framework.exchange.center.repository.StageRepository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class StageService {

  final StageRepository stageRepository;
  final StageConfigService stageConfigService;
  private final Snowflake snowflake;

  public StageService(
      StageRepository stageRepository,
      StageConfigService stageConfigService,
      Snowflake snowflake) {
    this.stageRepository = stageRepository;
    this.stageConfigService = stageConfigService;
    this.snowflake = snowflake;
  }

  public Page<Stage> getList(StageQueryDto queryDto) {

    QueryWrapper queryWrapper = new QueryWrapper();
    if (!StringUtils.isEmpty(queryDto.getStageName())) {

      queryWrapper.eq("stage_name", queryDto.getStageName());
    }

    PageHelper.startPage(queryDto.getCurrPage(), queryDto.getSize());
    List<Stage> stageList = stageRepository.selectList(queryWrapper);
    return ConvertUtils.convertPage(stageList);
  }

  public StageDetailDto getDetailDto(Long id) throws InvalidRequestParameterException {

    Stage stage = stageRepository.selectById(id);
    if (stage == null) {
      throw new InvalidRequestParameterException("所请求的环境不存在");
    }

    StageDetailDto stageDetailDto = new StageDetailDto();
    stageDetailDto.setStage(stage);
    QueryWrapper<StageConfig> stageConfigQueryWrapper = new QueryWrapper<>();
    stageConfigQueryWrapper.eq("stage_name", stage.getStageName());
    stageDetailDto
        .setStageConfig(stageConfigService.getBaseMapper().selectList(stageConfigQueryWrapper));
    return stageDetailDto;
  }

  public void updateStage(StageDetailDto stageDetailDto) throws InvalidRequestParameterException {

    QueryWrapper<Stage> stageQueryWrapper = new QueryWrapper<>();
    stageQueryWrapper.eq("stage_name", stageDetailDto.getStage().getStageName());
    List<Stage> stages = stageRepository.selectList(stageQueryWrapper);
    if (!stages.isEmpty() && !stages.get(0).getStageId()
        .equals(stageDetailDto.getStage().getStageId())) {

      throw new InvalidRequestParameterException("当前环境在数据库中已存在！");
    }

    stageRepository.updateById(stageDetailDto.getStage());

    //更新存在id的数据
    stageConfigService.updateBatchById(
        stageDetailDto.getStageConfig().stream().filter(p -> p.getConfigId() != null).collect(
            Collectors.toList()));

    //新增id不存在的数据
    stageDetailDto.getStageConfig().forEach(c -> {
      if (c.getConfigId() == null) {
        c.setConfigId(snowflake.nextId());
        stageConfigService.getBaseMapper().insert(c);
      }
    });
  }

  public void deleteStageConfig(Long id) {

    stageConfigService.getBaseMapper().deleteById(id);
  }

  public void insertDetailDto(StageDetailDto stageDetailDto)
      throws InvalidRequestParameterException {
    stageDetailDto.getStage().setStageId(snowflake.nextId());
    QueryWrapper<Stage> stageQueryWrapper = new QueryWrapper<>();
    stageQueryWrapper.eq("stage_name", stageDetailDto.getStage().getStageName());

    if (!stageRepository.selectList(stageQueryWrapper).isEmpty()) {
      throw new InvalidRequestParameterException("当前环境已经存在不允许再次新增!");
    }

    stageRepository.insert(stageDetailDto.getStage());
    //新增id不存在的数据
    stageDetailDto.getStageConfig().forEach(c -> {
      if (c.getConfigId() == null) {
        c.setConfigId(snowflake.nextId());
        c.setStageName(stageDetailDto.getStage().getStageName());
        stageConfigService.getBaseMapper().insert(c);
      }
    });
  }
}
